package com.xf.system.response;

import java.io.Serializable;
import java.util.Objects;

/**
 * 统一响应类
 * @author xiefengf
 * @param <T>
 */
public class ApiResult<T> implements Serializable {

    /**
     * 成功-常量
     */
    public static final String SUCCESS = "success";
    private static final Integer SUCCESS_CODE = 200;
    /**
     * 失败-常量
     */
    public static final String FAIL = "fail";

    /**
     * 返回码
     */
    private Integer code;

    /**
     * 返回信息说明
     */
    private String message;

    /**
     * 时间戳
     */
    private Long timestamp;

    /**
     * 额外的返回信息 - 可用于第三方对接时使用
     */
    private String extInfo;

    /**
     * 返回数据
     */
    private T data;

    /**
     * 成功/失败返回
     */
    public static <T> ApiResult<T> success() {
        return new ApiResult<T>(SUCCESS_CODE,SUCCESS);
    }
    public static <T> ApiResult<T> success(T data) {
        return new ApiResult<T>(SUCCESS_CODE, SUCCESS, data);
    }
    public static <T> ApiResult<T> success(T data,String extInfo) {
        return new ApiResult<T>(SUCCESS_CODE, SUCCESS,extInfo,data);
    }
    public static ApiResult<Object> fail(int code, String msg) {
        return new ApiResult<>(code, msg, "");
    }
    public static ApiResult<Object> fail(int code, String msg,String extInfo) {
        return new ApiResult<>(code, msg,extInfo,"");
    }
    /**
     * 自定义返回
     */
    public static <T> ApiResult<T> success(CodeAndMessage codeAndMessage,T data) {
        return new ApiResult<T>(codeAndMessage.getCode(),codeAndMessage.getMessage(), data);
    }
    public static <T> ApiResult<T> success(CodeAndMessage codeAndMessage,T data,String extInfo) {
        return new ApiResult<T>(codeAndMessage.getCode(),codeAndMessage.getMessage(),extInfo,data);
    }

    /**
     * 构造函数
     * @param code 状态码
     * @param message 返回信息
     */
    public ApiResult(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public ApiResult(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.timestamp = System.currentTimeMillis();
        this.data = data;
    }

    public ApiResult(Integer code, String message, String extInfo, T data) {
        this.code = code;
        this.message = message;
        this.timestamp = System.currentTimeMillis();
        this.extInfo = extInfo;
        this.data = data;
    }


    public int getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(Long timestamp) {
        this.timestamp = timestamp;
    }

    public String getExtInfo() {
        return extInfo;
    }

    public void setExtInfo(String extInfo) {
        this.extInfo = extInfo;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        ApiResult<?> apiResult = (ApiResult<?>) o;
        return Objects.equals(code, apiResult.code) && Objects.equals(message, apiResult.message) && Objects.equals(timestamp, apiResult.timestamp) && Objects.equals(extInfo, apiResult.extInfo) && Objects.equals(data, apiResult.data);
    }

    @Override
    public int hashCode() {
        return Objects.hash(code, message, timestamp, extInfo, data);
    }

    @Override
    public String toString() {
        return "ApiResult{" +
                "code=" + code +
                ", message='" + message + '\'' +
                ", timestamp=" + timestamp +
                ", extInfo='" + extInfo + '\'' +
                ", data=" + data +
                '}';
    }
}
